import validHtml from 'ngtemplate!html!../../../tpls/dialog/valid.dialog.html';

class RunnerEditController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $state, ngDialog, dialogService, CourseService, RunnerService, getRunnerDetail, getCourseList, iRunner){

    	var vm = this;

    	vm.$rootScope      = $rootScope;
    	vm.$cookieStore    = $cookieStore;
    	vm.$timeout 	   = $timeout;
    	vm.$state   	   = $state;
    	vm.ngDialog 	   = ngDialog;
    	vm.dialogService   = dialogService;
        vm.CourseService   = CourseService;
    	vm.RunnerService   = RunnerService;
    	vm.getRunnerDetail = getRunnerDetail;
        vm.getCourseList   = getCourseList;
    	vm.iRunner 	       = iRunner;

        var phone_exp    = /^\d{11}$/,
            identity_exp = /^[A-Za-z0-9]+$/,
            email_exp    = /^[_a-zA-Z0-9\-\.]+@([\-_a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/;
            
        // 表单数据初始化
        vm.runner = {
            runner_num: '',
            runner_name: '',
            runner_gender: 'male',
            runner_phone: '',
            runner_identity: '',
            runner_email: '',
            runner_address: '',
            runner_course: '',
            team: ''
        }
        
        // 获取选手详情
        if (vm.getRunnerDetail.code==0) {

        	var Detail = vm.getRunnerDetail.data;

            vm.runner.runner_num      = Detail.bib;
            vm.runner.runner_name     = Detail.runner_name;
            vm.runner.runner_gender   = Detail.runner_gender;
            vm.runner.runner_phone    = Detail.runner_cell;
            vm.runner.runner_identity = Detail.identity;
            vm.runner.runner_email    = Detail.runner_mail;
            vm.runner.runner_address  = Detail.address;
            
            // 科目列表
            if (vm.getCourseList.code==0) {

                vm.courselist = vm.getCourseList.data;

                //遍历取出当前选手科目信息
                angular.forEach(vm.getCourseList.data, function(value, index) {
                    if (Detail.course_id == value.course_id) {
                        vm.runner.runner_course = value;
                    }
                });

                angular.forEach(vm.teamlist, function(value, index) {
                    if (Detail.team_id == value.team_id) {
                        vm.runner.team = value;
                    }
                    console.log(vm.runner.team)
                });
            }
        }

        // 监听科目变化, 显示对应团队列表
        $scope.$watch(angular.bind(vm, () => vm.runner.runner_course), (newValue, oldValue) => {
            if (newValue) {
                vm.CourseService.getCourseTeam(vm.$cookieStore.get('access_token'), newValue.course_id)
                .then( data => {

                    data.code==0&&(vm.teamlist = data.data);
                    console.log(data)

                }, error => console.log('error'))
            } else if (newValue==undefined) {
                vm.teamlist = [];
            }
        });
        
        // 表单提交
        vm.submitForm = function(isValid) {

            // 验证失败, 提示用户
            if (!isValid) {
                
                if (!vm.runner.runner_course) {
                    $.blink(angular.element(document.getElementsByName('runnerCourse')), 5, 100)
                } else if (!vm.runner.runner_num) {
                    $.blink(angular.element(document.getElementsByName('runnerNum')), 5, 100)
                } else if (!vm.runner.runner_name) {
                    $.blink(angular.element(document.getElementsByName('runnerName')), 5, 100)
                } 
                
            }

            // 验证通过
            if (isValid) {
                
                if (vm.runner.runner_phone&&!phone_exp.test(vm.runner.runner_phone)) {
                    $.blink(angular.element(document.getElementsByName('runnerPhone')), 5, 100)
                    return false;
                } else if (vm.runner.runner_identity&&!identity_exp.test(vm.runner.runner_identity)) {
                    $.blink(angular.element(document.getElementsByName('runnerIdentity')), 5, 100)
                    return false;
                } else if (vm.runner.runner_email&&!email_exp.test(vm.runner.runner_email)) {
                    $.blink(angular.element(document.getElementsByName('runnerEmail')), 5, 100)
                    return false;
                }

                // 拼接POST数据格式
                var params = {
                    course_id: vm.runner.runner_course.course_id,
                    runner_name: vm.runner.runner_name,
                    runner_cell: vm.runner.runner_phone,
                    runner_mail: vm.runner.runner_email,
                    address: vm.runner.runner_address,
                    bib: vm.runner.runner_num,
                    runner_gender: vm.runner.runner_gender,
                    identity: vm.runner.runner_identity,
                    team_id: vm.runner.team ? vm.runner.team.team_id : ''
                }

                console.log(params)

                // 更新选手详情
                vm.RunnerService.postRunnerUpdate(params, vm.$cookieStore.get('access_token'), vm.$state.params.runner_id)
                .then(function(data) {

                    // 成功调用
                    data.code==0&&vm.dialogService.success('更新成功', 'dashboard.race.runnerlist');

                    // 调用失败, 提示用户
                    (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);

                    // 登录信息已过期, 请重新登录
                    data.code==-1001&&vm.dialogService.error(data.message, true);

                    console.log(data)
                }, function(data) {
                    console.log('error')
                });
            }

            return false;
        }

        // 删除选手信息
        vm.deleteItems = function(runner_id) {

            // 模态框显示文本
            $rootScope.common = {
                text : '是否删选手信息'
            }

            vm.ngDialog.open({
                template: validHtml,
                className: 'ngdialog-theme-default ngdialog-alert',
                scope: $scope
            });

            // 提交表单
            $rootScope.submitDelForm = function(isValid) {

                // 验证失败, 提示用户
                if (!isValid) {
                    vm.dialogService.error();
                }

                // 验证通过
                if (isValid) {

                    // 关闭模态框
                    ngDialog.close();

                    vm.RunnerService.postRunnerDel({runner_id: runner_id}, vm.$cookieStore.get('access_token'), vm.$state.params.race_id)
                    .then(function(data) {
                        
                        // 成功调用
                        data.code==0&&vm.dialogService.success('选手个人信息删除成功', 'dashboard.race.runnerlist');

                        // 调用失败, 提示用户
                        (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);

                        // 登录信息已过期, 请重新登录
                        data.code==-1001&&vm.dialogService.error(data.message, true);

                        console.log(data)
                    }, function(data) {
                        console.log('error')
                    });
                }

                return false;
            }
        }

    }
}

RunnerEditController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$state', 'ngDialog', 'dialogService', 'CourseService', 'RunnerService', 'getRunnerDetail', 'getCourseList', 'iRunner']; 

export default RunnerEditController;